<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { getList } from "/@/api/api/block/blockApi";
import { ElMessage } from "element-plus";
import { Search } from "@element-plus/icons-vue";
import TimeUtils from "/@/common/TimeUtils";
import StringUtils from "/@/common/StringUtils";

// 表格加载
const loading = ref(false);
// 表格数据
const tableData = ref([]);
// 总数据条数
const total = ref(0);
// 当前页码
const currentPage = ref(1);
// 可选的每页条数
const pageSizes = ref([10, 20, 50, 100]);
// 每页条数
const pageSize = ref(10);
// 检索词
const searchText = ref("");
const sortField = ref("BLOCK_CREATE_TIME");
const order = ref("DESC");

const router = useRouter();
// 每页展示条数更改回调
const handleSizePagChange = (val: any) => {
  pageSize.value = val;
  getTableList();
};

// 页面装载完成立刻执行
onMounted(() => {
  getTableList();
});
const headerCellStyle = () => {
  return {
    color: "#fff",
    fontWeight: "bold"
  };
};
const search = () => {
  currentPage.value = 1;
  getTableList();
};
// 获取数据列表
const getTableList = () => {
  loading.value = true;
  const params: any = {
    pageSize: pageSize.value,
    pageNum: currentPage.value,
    sortField: sortField.value,
    order: order.value
  };
  if (searchText.value) {
    params["blockHeight"] = searchText.value;
  }
  getList(params)
    .then((res: any) => {
      if (res.code === 0) {
        tableData.value = res.data.list;
        total.value = res.data.total;
      }
      loading.value = false;
    })
    .catch(() => {
      ElMessage({
        message: "查询异常，请稍后再试",
        type: "warning"
      });
    });

  loading.value = false;
};

// 翻页
const handleCurrentPagChange = (val: any) => {
  currentPage.value = val;
  getTableList();
};
const blockInfo = (val: any) => {
  router.push({
    name: "blockInfo",
    query: { height: val }
  });
};
const formatterDate = (row, column, cellValue) => {
  if (StringUtils.isNotEmpty(cellValue)) {
    return TimeUtils.dateFtt("yyyy-MM-dd hh:mm:ss", new Date(cellValue));
  } else {
    return cellValue;
  }
};
const handleSortChange = (val: any) => {
  sortField.value = val.prop;
  if (val.order === "descending") {
    order.value = "DESC";
  } else {
    order.value = "ASC";
  }
  getTableList();
};
</script>

<template>
  <el-card class="show-info">
    <div class="mt-4">
      <el-input
        placeholder="请输入区块高度"
        size="large"
        class="input-with-select"
        v-model="searchText"
      >
        <template #append>
          <el-button
            type="primary"
            size="large"
            :icon="Search"
            @click="search"
          ></el-button>
        </template>
      </el-input>
    </div>
    <div class="table-container">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        highlight-current-row
        :header-cell-style="headerCellStyle"
        @sort-change="handleSortChange"
      >
        <el-table-column prop="blockHeight" label="区块高度" width="100" />
        <el-table-column prop="blockHash" label="区块哈希" />
        <el-table-column prop="blockHashBase" label="区块BASE64哈希" />
        <el-table-column
          prop="blockCreateTime"
          label="出块时间"
          :formatter="formatterDate"
          sortable="custom"
          width="250"
        />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <span style="margin-right: 20px">
              <el-link type="primary" @click="blockInfo(scope.row.blockHeight)"
                >详细</el-link
              >
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: center">
        <el-pagination
          v-if="total > 10"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, prev, pager, next , sizes"
          :total="total"
          class="pagStyle"
          @size-change="handleSizePagChange"
          @current-change="handleCurrentPagChange"
        />
      </div>
    </div>
  </el-card>
</template>
<style lang="scss" scoped>
.show-info {
  min-height: 800px;
}

.mt-4 {
  padding: 30px;
  text-align: center;

  .input-with-select {
    width: 80%;
  }

  .el-button {
    width: 200px;
    background-color: var(--el-color-primary) !important;
  }
}

span {
  font-size: 14px;
}
</style>
<style lang="scss">
.table-container {
  padding: 30px;

  .el-table th {
    background-color: var(--el-color-primary);
    font-weight: 200;
  }
}
</style>
